﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Leo.Common.Json
{
    public static class JObjectExtensionFunctions
    {
        public static string GetString(this JObject jo, string propertyName)
        {
            JToken jtoken = jo[propertyName];
            if (jtoken == null)
            {
                return null;
            }
            if (jtoken.Type == JTokenType.Object || jtoken.Type == JTokenType.Array || jtoken.Type == JTokenType.Property)
            {
                return jtoken.ToString(0, Array.Empty<JsonConverter>());
            }
            return jtoken.ToString();
        }

        public static int? GetInt(this JObject jo, string propertyName)
        {
            string @string = jo.GetString(propertyName);
            if (string.IsNullOrEmpty(@string))
            {
                return default(int?);
            }
            int num;
            if (!int.TryParse(@string, out num))
            {
                return default(int?);
            }
            return new int?(num);
        }

        public static int GetInt(this JObject jo, string propertyName, int defaultValue)
        {
            int? @int = jo.GetInt(propertyName);
            if (@int == null)
            {
                return defaultValue;
            }
            return @int.GetValueOrDefault();
        }

        public static decimal? GetDecimal(this JObject jo, string propertyName)
        {
            string @string = jo.GetString(propertyName);
            if (string.IsNullOrEmpty(@string))
            {
                return default(decimal?);
            }
            decimal num;
            if (!decimal.TryParse(@string, out num))
            {
                return default(decimal?);
            }
            return new decimal?(num);
        }

        public static decimal GetDecimal(this JObject jo, string propertyName, decimal defaultValue)
        {
            decimal? @decimal = jo.GetDecimal(propertyName);
            if (@decimal == null)
            {
                return defaultValue;
            }
            return @decimal.GetValueOrDefault();
        }

        public static DateTime GetDateTime(this JObject jo, string propertyName)
        {
            return Convert.ToDateTime(jo.GetString(propertyName));
        }

        public static long? GetLong(this JObject jo, string propertyName)
        {
            string @string = jo.GetString(propertyName);
            if (string.IsNullOrEmpty(@string))
            {
                return default(long?);
            }
            long num;
            if (long.TryParse(@string, out num))
            {
                return new long?(num);
            }
            //throw new JsonFormatException("无法将JSON值[" + @string + "]转换为Long类型");
            throw new Exception("无法将JSON值[" + @string + "]转换为Long类型");
        }

        public static long GetLong(this JObject jo, string propertyName, long defaultValue)
        {
            long? @long = jo.GetLong(propertyName);
            if (@long == null)
            {
                return defaultValue;
            }
            return @long.GetValueOrDefault();
        }
    }
}
